const db = require('../db/index')

exports.getDoctor = (req,res)=>{
    const { name } = req.body
    let sql = 'select id, gender, name, email, phone, specialty, hospital_id from doctor where 1=1';
    let params = [];

    if (name) {
        sql += ' and name like ?';
        params.push(`%${name}%`);
    }

    db.query(sql, params, (err,result)=>{
        if(err){
            return res.send({
                status: 1,
                msg: '查询失败了'+err
            })
        }
        console.log('结果：',result)
        return res.send({
            status: 0,
            msg: '查询成功',
            data: result
        })
    })
}

exports.addDoctor = (req,res)=>{
    const { name, gender, specialty, hospital_id, position, email, phone, } = req.body;
    let params = { name, gender, specialty, hospital_id }
    console.log(params)
    if(!name||!gender||!specialty||!hospital_id){
        return res.send({
            status: 1,
            msg: '信息不能为空！'
        })
    }
    if(position){
        params.position = position
    }
    if(email){
        params.email = email
    }
    if(phone){
        params.phone = phone
    }
    db.query('insert into doctor set ?', params, (err, result) => {
        if (err) {
            return res.send({
                status: 1,
                message: err.message
            });
        }
        db.query('update department set doctors_count = doctors_count + 1 where id = ? and name = ?', [params.hospital_id, params.specialty], (err, result) => {
            if (err) {
                return res.send({
                    status: 1,
                    message: err.message
                });
            }
            res.send({
                status: 0,
                message: '添加成功'
            });
        });
    })
}

exports.updateDoctor = (req, res) => {
    const { id, name, gender, specialty, hospital_id, position, email, phone } = req.body;
    console.log(req.body)

    // 检查所有必需字段
    if (!id || !name || !gender || !specialty || !hospital_id) {
        return res.status(400).send({
            status: 1,
            msg: '信息不能为空！'
        });
    }

    let sql = 'UPDATE doctor SET name=?, gender=?, specialty=?, hospital_id=?, position=?, email=?, phone=? WHERE id=?';
    let sqlParams = [name, gender, specialty, hospital_id, position, email, phone, id];

    db.query(sql, sqlParams, (err, result) => {
        if (err) {
            console.error('数据库更新错误:', err);
            return res.status(500).send({
                status: 1,
                msg: '更新失败了，请稍后再试'
            });
        }

        if (result.affectedRows === 0) {
            return res.status(404).send({
                status: 1,
                msg: '医生不存在，更新失败'
            });
        }

        return res.status(200).send({
            status: 0,
            msg: '更新成功'
        });
    });
}

exports.deleteDoctor = (req, res) => {
    const { id, hospital_id, specialty } = req.body;
    console.log(req.body)
    if (!id) {
        return res.status(400).send({
            status: 1,
            msg: '医生ID不能为空！'
        });
    }

    db.query('delete from doctor where id = ?', id, (err, result) => {
        if (err) {
            return res.send({
                status: 1,
                message: err.message
            });
        }
        db.query('update department set doctors_count = doctors_count - 1 where id = ? and name = ?', [hospital_id, specialty], (err, result) => {
            if (err) {
                return res.send({
                    status: 1,
                    message: err.message
                });
            }
            res.send({
                status: 0,
                message: '删除成功'
            });
        });
    });
}
